package com.leyou.upload.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author CHERRY
 * @date 2018-09-03-9:18
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration configuration = new CorsConfiguration();

        //允许请求方式
        configuration.addAllowedMethod("POST");
        configuration.addAllowedMethod("OPTIONS");
        //允许请求头
        configuration.addAllowedHeader("*");
        //允许的域
        configuration.addAllowedOrigin("http://manage.leyou.com");
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        //映射路径 拦截一切请求
        configurationSource.registerCorsConfiguration("/**", configuration);
        return new CorsFilter(configurationSource);
    }

}
